import { defineComponent, computed, watch } from "vue";
import defaultProps from "../props.mjs";
const name = "ep-upperamount";
const _sfc_main = defineComponent({
  name,
  props: defaultProps,
  emits: ["update:modelValue"],
  setup(props, { emit }) {
    const isDetail = computed(() => props.options.permission > 0);
    const fieldModel = computed({
      get() {
        if (isDetail.value && !props.modelValue) {
          return "--";
        }
        return props.modelValue;
      },
      set(val) {
        emit("update:modelValue", val);
      }
    });
    let timeout;
    watch(
      () => props.formData,
      (data) => {
        const { selectSumName } = props.options;
        if (selectSumName) {
          clearTimeout(timeout);
          timeout = setTimeout(() => {
            if (data[selectSumName]) {
              const num = amountToChinese(data[selectSumName]);
              emit("update:modelValue", num);
            } else {
              emit("update:modelValue", "");
            }
          }, 50);
        }
      },
      { deep: true }
    );
    function amountToChinese(amount) {
      const cnNums = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
      const cnIntRadice = ["", "拾", "佰", "仟"];
      const cnIntUnits = ["", "万", "亿", "兆"];
      const cnDecUnits = ["角", "分"];
      const cnInteger = "整";
      const cnIntLast = "元";
      const maxNum = props.options.max || Number("999999999999999.99");
      let integerNum;
      let decimalNum;
      let chineseStr = "";
      let parts;
      if (typeof amount !== "string") {
        amount = amount.toString();
      }
      if (amount === "") {
        return "";
      }
      let isNegative = false;
      if (amount.startsWith("-")) {
        isNegative = true;
        amount = amount.slice(1);
      }
      if (amount.indexOf(".") === -1) {
        integerNum = amount;
        decimalNum = "";
      } else {
        parts = amount.split(".");
        integerNum = parts[0];
        decimalNum = parts[1].substr(0, 2);
      }
      if (integerNum > maxNum) {
        return "";
      }
      if (parseInt(integerNum, 10) > 0) {
        let zeroCount = 0;
        const IntLen = integerNum.length;
        for (let i = 0; i < IntLen; i++) {
          const n = integerNum.substr(i, 1);
          const p = IntLen - i - 1;
          const q = Math.floor(p / 4);
          const m = p % 4;
          if (n === "0") {
            zeroCount++;
          } else {
            if (zeroCount > 0) {
              chineseStr += cnNums[0];
            }
            zeroCount = 0;
            chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
          }
          if (m === 0 && zeroCount < 4) {
            chineseStr += cnIntUnits[q];
          }
        }
        chineseStr += cnIntLast;
      }
      if (decimalNum !== "") {
        const decLen = decimalNum.length;
        for (let i = 0; i < decLen; i++) {
          const n = decimalNum.substr(i, 1);
          if (n !== "0") {
            chineseStr += cnNums[Number(n)] + cnDecUnits[i];
          }
        }
      }
      if (chineseStr === "") {
        chineseStr += cnNums[0] + cnIntLast + cnInteger;
      } else if (decimalNum === "") {
        chineseStr += cnInteger;
      }
      if (isNegative) {
        chineseStr = "负" + chineseStr;
      }
      return chineseStr;
    }
    return {
      fieldModel,
      isDetail
    };
  }
});
export {
  _sfc_main as default
};
